#include "laser.h"

void Laser::draw() {
	float direction = dir_ * rToDeg;

	glTranslatef(pos_.x, pos_.y, 0.0);
	glRotatef(direction, 0.0, 0.0, 1.0);

	glColor4f(r, g, b, 1.0);
	
	glBegin(GL_POLYGON); {
		for (int i = 0; i < numVertii_; i++) {
			glVertex2f(vertii_[i].x, vertii_[i].y);
		}
	} 
	glEnd();
	
	if(on) {
    	glBegin(GL_POLYGON); {
    	    glColor4f(baR, baG, baB, baA);
    		glVertex2f(blastVertii_[0].x, blastVertii_[0].y);
    		glColor4f(bbR, bbG, bbB, bbA);
    		glVertex2f(blastVertii_[1].x, blastVertii_[1].y);
			glColor4f(baR, baG, baB, baA);
    		glVertex2f(blastVertii_[2].x, blastVertii_[2].y);
    		glVertex2f(blastVertii_[3].x, blastVertii_[3].y);
    		glVertex2f(blastVertii_[4].x, blastVertii_[4].y);
    		glColor4f(bbR, bbG, bbB, bbA);
			glVertex2f(blastVertii_[5].x, blastVertii_[5].y);
    		glColor4f(baR, baG, baB, baA);
    		glVertex2f(blastVertii_[6].x, blastVertii_[6].y);
    	} 
    	glEnd();	
	}

	glRotatef(-direction, 0.0, 0.0, 1.0);
	glTranslatef(-pos_.x, -pos_.y, 0.0);
}
    
void Laser::update(float step) {
    if(on) {
    	cpVect pos = cpvadd(pos_, firingOffset);
		float hostDirFloat = getHost()->a;
		cpVect hostDir = cpvforangle(hostDirFloat);
		
		GameObject* pB = new LaserBeam(5.0,
    								   cpvadd(getHost()->p, cpvrotate(cpvrotate(pos, dirVect_), hostDir)),
    								   dir_ + hostDirFloat,
    								   cpvadd(getHost()->v, cpvrotate(cpvrotate(vel_, dirVect_), hostDir)));
		
		
    	spawnObject(pB);
	}
}
